#ifndef FFMUXER_H
#define FFMUXER_H

#include<iostream>
#include<mutex>
#include<atomic>
#include<chrono>
#include<thread>
extern "C"
{
#include<libavformat/avformat.h>
}
class FFMuxer{
public:
    FFMuxer();
    ~FFMuxer();

    void init(std::string const& url_);
    void addStream(AVCodecContext* codecCtx);
    int mux(AVPacket* packet);
    void writeHeader();
    void writeTrailer();

    int getAStreamIndex();
    int getVStreamIndex();
private:
    void initMuxer();
    void printError(int ret);

private:
    std::string url;

    AVFormatContext* fmtCtx = nullptr;

    AVCodecContext* aCodecCtx = nullptr;
    AVCodecContext* vCodecCtx = nullptr;

    AVStream* aStream = nullptr;
    AVStream* vStream = nullptr;
    int aStreamIndex = -1;
    int vStreamIndex = -1;

    std::mutex mutex;
    std::atomic<bool>headerFlag;
    std::atomic<bool>trailerFlag;
    std::atomic<bool>readyFlag;
    int streamCount = 0;
};







#endif //FFMUXER_H
